package com.teadent.util;

/*import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionFactory {
    //获取数据库
    private static DataSource dataSource=new ComboPooledDataSource();
    //获取链接
    public static Connection getConnection(){
        try{
            return dataSource.getConnection();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
    //资源关闭工具方法
    public static void close (Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        try{
            if(resultSet!=null) resultSet.close();
            if(preparedStatement!=null)preparedStatement.close();
            if(connection!=null) connection.close();
;        }catch (SQLException e){
            e.printStackTrace();
        }
    }

}*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionFactory {
    private static Connection connection;

    private ConnectionFactory() {
    }



    public static Connection getConnection() {
        if (connection == null) {
            try {
                String driverClassName = "com.mysql.cj.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3306/example";
                String username = "root";
                String password = "1234";

                Class.forName(driverClassName);
                connection = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}